﻿using System;
using System.Drawing;
using System.Windows.Forms;
using S7PlcDataCollector.Client.Controls;
using S7PlcDataCollector.Client.Tools;
using S7PlcDataCollector.Service;
using ZbSkin.Frm;

namespace S7PlcDataCollector.Client
{
    public partial class FormMain : SkinMainForm
    {
        private readonly MenuControl _menuControl;
        private readonly Panel _customPanel;

        private readonly PageControl _plcListPage;
        private readonly PageControl _realtimeDataPage;
        private PageControl _curPage;

        public FormMain()
        {
            InitializeComponent();

            _menuControl = new MenuControl() {BackColor = ContentPanel.BackColor, Width = 120};
            _menuControl.SetAlignment(MenuControl.MenuItemAlignment.Vertical);
            _menuControl.MenuItemClick += OnMenuItemClick;
            _customPanel = new Panel() {BackColor = ContentPanel.BackColor};

            _plcListPage = new PlcListControl() { Visible = false, Width = Constants.ContentWidth };
            _realtimeDataPage = new RealtimeDataControl() { Visible = false, Width = Constants.ContentWidth };
            _customPanel.Controls.Add(_plcListPage);
            _customPanel.Controls.Add(_realtimeDataPage);

            ContentPanel.Controls.Add(_menuControl);
            ContentPanel.Controls.Add(_customPanel);
            ContentPanel.SizeChanged += OnContentPanelSizeChanged;
            BottomPanel.Controls.Add(new BottomControl() { Dock = DockStyle.Fill });
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            Title = "西门子S7系列PLC数据采集工具";
            ResizeContentPanelControls();

            S7PlcDataCollectorService.Instance.Init();
        }

        protected override void DoFormClosed()
        {
            S7PlcDataCollectorService.Instance.UnInit();
            base.DoFormClosed();
        }

        private void FormMain_SizeChanged(object sender, EventArgs e)
        {

        }

        private void OnContentPanelSizeChanged(object sender, EventArgs e)
        {
            ResizeContentPanelControls();
        }

        private void ResizeContentPanelControls()
        {
            _menuControl.Height = ContentPanel.ClientSize.Height;
            _menuControl.Left = 0;
            _menuControl.Top = 0;

            _customPanel.Width = ContentPanel.ClientSize.Width - _menuControl.Width;
            _customPanel.Height = ContentPanel.ClientSize.Height;
            _customPanel.Left = _menuControl.Right;
            _customPanel.Top = 0;

            var pageWidth = Constants.ContentWidth < _customPanel.Width - 40
                ? Constants.ContentWidth
                : _customPanel.Width - 40;

            foreach (Control control in _customPanel.Controls)
            {
                if (control != null)
                {
                    control.Width = pageWidth;
                    control.Height = _customPanel.ClientSize.Height - 20;
                    control.Left = (_customPanel.ClientSize.Width - control.Width) / 2;
                    control.Top = 10;
                }
            }
        }

        private void OnMenuItemClick(int menuTag)
        {
            switch (menuTag)
            {
                case Constants.MenuTag.PlcList:
                    SetCurPage(_plcListPage);
                    break;
                case Constants.MenuTag.RealtimeData:
                    SetCurPage(_realtimeDataPage);
                    break;
            }
        }

        private void SetCurPage(PageControl control)
        {
            control.ShowControl(true);
            _curPage?.ShowControl(false);
            _curPage = control;

            var pageWidth = Constants.ContentWidth < _customPanel.Width - 40
                ? Constants.ContentWidth
                : _customPanel.Width - 40;
            control.Width = pageWidth;
            control.Height = _customPanel.ClientSize.Height - 20;
            control.Left = (_customPanel.ClientSize.Width - control.Width) / 2;
            control.Top = 10;
        }
    }
}
